package com.wyu.notaryserver.util;

import java.lang.reflect.InvocationTargetException;

/**
 * @author 六味lemontea 2025-01-25
 * @version 1.0
 * @description
 */
public class InfoEnumUtil {

    public static Integer getCodeByDesc(String desc, Class<?> clazz) {
        if (clazz.isEnum()) {
            for (Object enumObj : clazz.getEnumConstants()) {
                try {
                    java.lang.reflect.Field descField = clazz.getDeclaredField("desc");
                    descField.setAccessible(true);
                    String enumDesc = (String) descField.get(enumObj);
                    if (enumDesc.equals(desc)) {
                        java.lang.reflect.Method getCodeMethod = clazz.getMethod("getEnumValue");
                        return (Integer) getCodeMethod.invoke(enumObj);
                    }
                } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException |
                         InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
